package com.myself.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author W.S.
 * @version v4.0.0
 * @description 延时队列配置
 * @date Created in 2018/8/27 16:49
 */
@Configuration
public class DelayedQueueConfig {


    /**
     * 用于转发私信的对列
     */
    public static final String QUEUE_1 = "QUEUE_1";

    /**
     * 用于处理延时转发过来的消息的队列
     */
    public static final String QUEUE_2 = "QUEUE_2";
    /**
     * 用于转发私信的对列
     */
    public static final String QUEUE_3 = "QUEUE_3";

    /**
     * 用于转发私信的对列
     */
    @Bean
    public Queue queue1(){
        Map<String, Object> arguments = new HashMap<>();
        //队列消息的存活时间ttl
        arguments.put("x-message-ttl", 60000);
        arguments.put("x-dead-letter-exchange", "DEAD_LETTER_EX");
        arguments.put("x-dead-letter-routing-key", "dead.letter");
        return new Queue(QUEUE_1,true, false, false,arguments);
    }

    /**
     * 死信交换机
     * @return
     */
    @Bean
    public DirectExchange deadLetterExchange(){
        return new DirectExchange("DEAD_LETTER_EX");
    }

    /**
     * 用于处理延时转发过来的消息的队列
     */
    @Bean
    public Queue queue2(){
        return new Queue(QUEUE_2);
    }

    /**
     * 用于接口设置的失效时间的消息的队列
     */
    @Bean
    public Queue queue3(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "DEAD_LETTER_EX");
        arguments.put("x-dead-letter-routing-key", "dead.letter");
        return new Queue(QUEUE_3,true, false, false,arguments);
    }

    @Bean
    public Binding bindExchange_1(Queue queue1,DirectExchange deadLetterExchange){
        return BindingBuilder.bind(queue1).to(deadLetterExchange).with("send.msg");
    }

    @Bean
    public Binding bindExchange_2(Queue queue2,DirectExchange deadLetterExchange){
        return BindingBuilder.bind(queue2).to(deadLetterExchange).with("dead.letter");
    }

    @Bean
    public Binding bindExchange_3(Queue queue3,DirectExchange deadLetterExchange){
        return BindingBuilder.bind(queue3).to(deadLetterExchange).with("send.expration.msg");
    }

}
